package org.example.classloader;

import sun.net.spi.nameservice.dns.DNSNameService;

public class ClassLoaderTest {


    public static void main(String[] args) throws Exception {

        /*getSysEnv();

        getClassLoader();

        classLoaderTree(ClassLoaderTest.class);*/

        /*String classPath = "org.example.classloader.a.b.c.test.BService";
        String classDir = "/Users/liuyicai/Documents/work/project/test-xuanwu-programming/other/BService.class";
        FileClassLoader fileClassLoader = new FileClassLoader(classDir);
        Class<?> aClass = fileClassLoader.loadClass(classPath);
        classLoaderTree(aClass);
        IService service = (IService) aClass.newInstance();
        System.out.println(service.hello("happy new year!"));*/


    }

    private static void classLoaderTree(Class<?> clazz) {

        ClassLoader classLoader = clazz.getClassLoader();
        while (classLoader != null) {
            System.out.println(classLoader);
            classLoader = classLoader.getParent();
        }
    }

    public static void getClassLoader() {

        System.out.println(String.class.getClassLoader());

        System.out.println(DNSNameService.class.getClassLoader());

        System.out.println(ClassLoaderTest.class.getClassLoader());

        try {
            Class.forName("");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void getSysEnv() {
        // jre/lib、jar/classes
        System.out.println(System.getProperty("sun.boot.class.path"));
        System.out.println(System.getProperty("java.ext.dirs"));
        System.out.println(System.getProperty("java.class.path"));
        System.out.println(System.getProperty("java.home"));
    }
}
